package com.cloudeasy.area.controller;

import com.cloudeasy.api.constant.PermissionConstant;
import com.cloudeasy.api.enums.ResultCodeEnum;
import com.cloudeasy.api.util.ResultUtil;
import com.cloudeasy.api.vo.area.AreaRelyTreeVO;
import com.cloudeasy.api.vo.Result;
import com.cloudeasy.api.vo.area.AreaRelyVO;
import com.cloudeasy.area.constant.AreaConstant;
import com.cloudeasy.area.po.Area;
import com.cloudeasy.area.service.AreaRelyService;
import com.cloudeasy.area.service.AreaService;
import com.cloudeasy.area.util.AreaRelyUtil;
import com.cloudeasy.core.bean.JwtParser;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
public class AreaRelyController {
    private final AreaService areaService;

    private final AreaRelyService areaRelyService;

    private final JwtParser jwtParser;

    public AreaRelyController(AreaService areaService, AreaRelyService areaRelyService, JwtParser jwtParser) {
        this.areaService = areaService;
        this.areaRelyService = areaRelyService;
        this.jwtParser = jwtParser;
    }

    /**
     * 添加子区域
     *
     * @param parentId 父区域Id
     * @param childId  子区域Id
     * @return {@link Result<AreaRelyVO>}
     */
    @PostMapping("/append/{parentId}/{childId}")
    public Result<AreaRelyVO> add(Authentication authentication, @PathVariable("parentId") String parentId, @PathVariable("childId") String childId) {
        List<String> targetAreas = areaService.listByIds(Arrays.asList(childId, parentId)).stream().map(Area::getName).collect(Collectors.toList());
        String userId = (String) jwtParser.getOnBody(authentication, "userId");
        if (areaRelyService.canDo(PermissionConstant.ADD_CHILD_AREA, targetAreas, userId)) {
            return ResultUtil.success(AreaRelyUtil.convert(areaRelyService.save(parentId, childId)));
        }
        return ResultUtil.failure(ResultCodeEnum.NO_PERMISSION_OPERATE);
    }

    /**
     * 获取根区域及子区域树型结构
     *
     * @return {@link Result<AreaRelyTreeVO>}
     */
    @GetMapping("/tree")
    public Result<AreaRelyTreeVO> tree() {
        return ResultUtil.success(areaRelyService.tree(AreaConstant.GLOBAL_ROOT_AREA_ID));
    }

    /**
     * 获取区域及子区域树型结构
     *
     * @param areaId 区域Id
     * @return {@link Result<AreaRelyTreeVO>}
     */
    @GetMapping("/tree/{areaId}")
    public Result<AreaRelyTreeVO> tree(@PathVariable(value = "areaId") String areaId) {
        return ResultUtil.success(areaRelyService.tree(areaId));
    }

    /**
     * 判断父区域列表是否可以包含所有子区域列表
     *
     * @param parentNames 父区域列表
     * @param childNames  子区域列表
     * @return {@link Result<Boolean>}
     */
    @GetMapping("/is_sub_area")
    public Result<Boolean> isSubArea(@RequestParam("parentNames") List<String> parentNames,
                                     @RequestParam("childNames") List<String> childNames) {
        return ResultUtil.success(areaRelyService.isSubArea(parentNames, childNames));
    }
}
